﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ThreadingWinApp
{
    public partial class Form2 : Form
    {
        
        BackgroundWorker _thread = new BackgroundWorker();
        //private ProgressChangedEventHandler ProgressChanged;
        public Form2()
        {
            InitializeComponent();
            _thread.WorkerReportsProgress = true;
            _thread.DoWork += new DoWorkEventHandler(DoWork);
            _thread.ProgressChanged += new ProgressChangedEventHandler(PrograssChanged);
        }
        
        delegate void UpdatePrograssbarDeleagte(int arg);
        private void UpdatePrograssbar(int arg)
        {
            if(this.InvokeRequired)
                this.BeginInvoke(new UpdatePrograssbarDeleagte(UpdatePrograssbar),new object[]{arg});
            else
                progressBar1.Value=arg;
        }
        void PrograssChanged(object sender, ProgressChangedEventArgs e)
        {
            UpdatePrograssbar(e.ProgressPercentage);
        }
        delegate void UpdateFileNameDelegate(string fileName);
        private void DisplayFileName(string fileName)
        {
            if (this.InvokeRequired)
            {
                UpdateFileNameDelegate delegate1 = new UpdateFileNameDelegate(DisplayFileName);
                object[] parameters = new object[1] { fileName };
                this.BeginInvoke(delegate1, parameters);
            }
            else
                label1.Text = fileName;
        }

        string _sourceDirectoryName = @"D:\Test\Source";
        string _destinationDirectoryName = "D:\\Test\\Destination";

        void DoWork(object sender, DoWorkEventArgs e)
        {
            if (Directory.Exists(_sourceDirectoryName))
            {
                int count = 1;
                int totalFileCount = Directory.GetFiles(_sourceDirectoryName).Length;
                foreach (string sourcefilename in Directory.GetFiles(_sourceDirectoryName))
                {
                    string fileName = sourcefilename.Substring((sourcefilename.LastIndexOf('\\') + 1));
                    DisplayFileName(fileName);
                    CopyFile(sourcefilename, Path.Combine(_destinationDirectoryName, fileName));
                    float Percentage = (count / (float)totalFileCount) * 100;
                    count++;
                    _thread.ReportProgress((int)Percentage);
                }
            }
      
            //int Max = 100;
            //for (int i = 0; i < Max; i++)
            //{
            //    float Percentage = (i / (float)Max) * 100;
            //    _thread.ReportProgress((int)Percentage);
            //    //label2(string.Format("{0}%",Percentage);
            //}
        }

        private void CopyFile(string SourceFileName, string DestinationFileName)
        {
            try
            {
                System.IO.File.Copy(SourceFileName, DestinationFileName,true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _thread.RunWorkerAsync();

        }
    }
}
